package net.daum.android.lecture4.homework;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.homework4.R;

public class MyCalendarView extends LinearLayout {
	private View oldSelectedTextView;
	private Drawable oldSelectedDrawable;

	public MyCalendarView(Context context) {
		super(context);
		this.init(context);
	}

	public MyCalendarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init(context);
	}

	public void init(Context context) {
		RelativeLayout root = new RelativeLayout(context);
		root.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		root.addView(createTableLayout(context));
		this.addView(root);
	}

	private TableLayout createTableLayout(Context context) {
		Calendar calendar = Calendar.getInstance();
		
		//calendar.add(Calendar.MONTH, 2);
		
		Activity activity = (Activity) context;
		activity.setTitle(calendar.get(Calendar.YEAR) + this.getResources().getString(R.string.year) + " " + (calendar.get(Calendar.MONTH) + 1) + this.getResources().getString(R.string.month));
		
		int today = calendar.get(Calendar.DAY_OF_MONTH);
		
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		Log.d("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
		
		int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		Log.d("firstDayOfWeek", String.valueOf(firstDayOfWeek));
		
		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.MONTH, -1);
		int maxDayOfPreviousMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		Log.d("day", String.valueOf(maxDayOfPreviousMonth));
		int day = 1;
		
		if (firstDayOfWeek != 1) {
			day = maxDayOfPreviousMonth - firstDayOfWeek + 2;
		}
		Log.d("day", String.valueOf(day));
		
		TableLayout tableLayout = new TableLayout(context);
		RelativeLayout.LayoutParams tableLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		tableLayoutParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
		tableLayoutParam.addRule(RelativeLayout.CENTER_VERTICAL);
		tableLayout.setStretchAllColumns(true);
		tableLayout.setLayoutParams(tableLayoutParam);
		
		TableRow headerTableRow = new TableRow(context);
		TableLayout.LayoutParams tableRowLayoutParam = new TableLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 20, 0.3f);
		headerTableRow.setLayoutParams(tableRowLayoutParam);
		
		for (int i = 0; i < 7; i++) {
			TableRow.LayoutParams textViewLayoutParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
			TextView textView = new TextView(context);
			
			if (i == 0) {
				textView.setText(R.string.sun);
			} else if (i == 1) {
				textView.setText(R.string.mon);
			} else if (i == 2) {
				textView.setText(R.string.tue);
			} else if (i == 3) {
				textView.setText(R.string.wed);
			} else if (i == 4) {
				textView.setText(R.string.thu);
			} else if (i == 5) {
				textView.setText(R.string.fri);
			} else if (i == 6) {
				textView.setText(R.string.sat);
			}
			textView.setLayoutParams(textViewLayoutParam);
			textView.setGravity(Gravity.CENTER);
			
			headerTableRow.addView(textView);
		}
		
		tableLayout.addView(headerTableRow);
		
		//int day = 30;
		boolean isValidDay = false;
		
		for (int i = 0; i < 6; i++) {
			TableRow dayTableRow = new TableRow(context);
			TableLayout.LayoutParams dayTableRowLayoutParam = new TableLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0f);
			dayTableRow.setLayoutParams(dayTableRowLayoutParam);
			
			for (int j = 0; j < 7; j++) {
				TableRow.LayoutParams textViewLayoutParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
				
				TextView textView = new TextView(context);
				textView.setLayoutParams(textViewLayoutParam);
				textView.setText(String.valueOf(day));
				textView.setGravity(Gravity.RIGHT);
				textView.setBackgroundResource(R.drawable.valid_border);
				textView.setPadding(0, 10, 10, 0);
				
				if (isValidDay) {
					if (today == day) {
						textView.setBackgroundResource(R.drawable.today_border);
					} else {
						textView.setBackgroundResource(R.drawable.valid_border);
					}
				} else {
					textView.setBackgroundResource(R.drawable.invalid_border);
				}
				
				textView.setOnClickListener(new OnClickListener() {
					
					@SuppressLint("NewApi")
					@Override
					public void onClick(View v) {

						if (oldSelectedTextView != null) {
							oldSelectedTextView.setBackground(oldSelectedDrawable);
						}

						oldSelectedDrawable = v.getBackground();
						oldSelectedTextView = v;
						v.setBackgroundResource(R.drawable.selection_border);
					}
				});
				
				dayTableRow.addView(textView);
				
				day++;
				
				boolean isChangedMonth = false;
				
				if (!isValidDay) {
					if (day > maxDayOfPreviousMonth) {
						isChangedMonth = true;
					}
				} else {
					if (day > maxDay) {
						isChangedMonth = true;
					}
				}
				
				if (isChangedMonth) {
					day = 1;
					isValidDay = !isValidDay;
				}
			}
			
			tableLayout.addView(dayTableRow);
		}
		
		return tableLayout;
	}
}